<%@ page language="java"
	import="java.util.*,cn.edu.cuit.elena.transaction.system.UserContext;"
	pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP starting page</title>
<link rel="stylesheet" type="text/css"
	href="js/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="style/mystyle.css" />
<script type="text/javascript" src="js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext/ext-all.js"></script>
<script>
	Ext
			.onReady(function() {
				var dataSourcePanel;
				var b_username = "null";
				var b_url = "null";
				var b_password = "null";
				var b_driver = "null";
				var b_schemaname = "null";
				var flag;
				var userContext = "<%=((UserContext) session.getAttribute("usercontext")).getIdentifier()%>";
				var cpath = "null";
				var upath = "";

				// 				var profilePanel = new Ext.tree.TreePanel({
				// 					animate : true,
				// 					frame : true,
				// 					region : 'center',
				// 					margins : '5 0 5 5',
				// 					rootVisible : true,
				// 					autoScroll : true,
				// 					width : '100%',
				// 					lines : true,
				// 					loader : new Ext.tree.TreeNode({
				// 						dataUrl : 'hdfsview.svt?kind=hdfs'
				// 					}),
				// 					root : new Ext.tree.AsyncTreeNode({
				// 						text : 'ROOT',
				// 						id : 'root',
				// 						expanded : true
				// 					})
				// 				});

				var profilePanel = new Ext.tree.TreePanel({
					region : 'center',
					title : '分布式文件系统目录',
					frame : true,
					autoScroll : true,
					margins : '5 0 5 5',
					lines : true,
					height : 300,
					loader : new Ext.tree.TreeLoader({
						dataUrl : 'hdfsview.svt?kind=hdfs'
					}),
					root : new Ext.tree.AsyncTreeNode({
						text : 'HDFS ROOT'
					})
				});

				profilePanel.on("click", function(tree, root, s) {
					var flag_1 = false;
					if(tree.leaf == "true"){
						flag_1 = true;
					}
					if (tree.text == "HDFS ROOT") {
						return;
					}

					// 						if(tree.leaf == "true"){
					// 							tree = tree.parentNode;
					// 						}
					// 					var p_1 = tree.parentNode;
					// 					var p_2 = p_1.parentNode;
					// 					hdfsPath = "/user/quintus/" +userContext + "/" + p_2.text
					// 							+ "/" + p_1.text;

					// 					hdfsfile = "/user/quintus/" + userContext + "/" + p_2.text
					// 							+ "/" + p_1.text+"/"+tree.text;
					// 										alert(hdfsPath+" "+hdfsfile);

					var paths = new Array();
					var temp = tree;
					while (true) {
						if (tree.text == "HDFS ROOT") {
							break;
						} else {
							paths.push(tree.text);
// 							alert(tree.text);
							tree = tree.parentNode;
						}
					}
					cpath = "/user/quintus/" + userContext + "/";
					upath = "/";
					paths.reverse();
					for ( var a in paths) {
						if (a == "remove")
							break;
						cpath = cpath + paths[a] + "/";
						upath = upath + paths[a] + "/";
					}
// 														alert(cpath);

					if(flag_1){
					var tmp = Ext.getCmp("dataSourcePanel");
// 					alert(tmp.getStore().proxy.conn.url);
					dataSourcePanel.getStore().proxy.conn.url = 'hdfsview.svt?kind=filedata&path='+cpath;
// 					alert(dataSourcePanel.getStore().proxy.conn.url);
					var store = tmp.getStore();
					store.reload();
					}
				});

				/////////////////////////////////////////////////////////////buttons
				var buttons = new Ext.Panel(
						{
							frame : true,
							region : 'east',
							margins : '5 0 5 5',
							width : 115,
							items : [
									new Ext.Button(
											{
												text : "从本地复制",
												width : 100,
												height : 45,
												handler : function() {
													var path;
													Ext.MessageBox
															.prompt(
																	"Prompt",
																	"请输入想要导入到本地哪个目录，目录结构如下 \"DirA/DirB/FileC\":",
																	function(
																			button,
																			text) {
																			path = text;
																		if (button == "ok") {
																			// 																			alert("您的新年愿望是:"
																			// 																					+ text);
																			var localpath = "null";
																			Ext.MessageBox
																					.prompt(
																							"Prompt",
																							"                  请输入本地文件系统文件路径:             ",
																							function(
																									button,
																									text) {
																								localpath = text;
																								if(text=="")return;
																								if (localpath == "null")
																									return;

																			var requestConfig = {
																				url : 'hdfsview.svt?kind=movefromlocal',
																				params : {
																					tar : path
																					,
																					src:localpath
																				},
																				callback : function(
																						option,
																						success,
																						response) {
																					var msg = [
																							success,
																							option.customer,
																							response.responseText];
																					if(response.responseText == "1"){
																						Ext.MessageBox.alert("Warn!","Create Success!");
																							profilePanel.getRootNode().reload();
																					}else{
																						Ext.MessageBox.alert("Warn!","Create failed!");
																					}
																					//grid.getStore().reload();
																				}
																			}
																			Ext.Ajax
																					.request(requestConfig);
																							});

																		} else
																			// 																			alert("你放弃了录入！");
																			return;
																	});

												}

											}),
									
									/*new Ext.Button({
										text : "Refresh",
										width : 100,
										height : 25,
										handler : function() {///////////////////////////
										}
									}),*/
									new Ext.Button(
											{
												text : "复制到本地",
												width : 100,
												height : 45,
												handler : function() {
													if(cpath == "null"){
													Ext.MessageBox.alert("Prompt","请选择一个文件");
													return;
													}
													Ext.MessageBox.prompt("Prompt","请输入一个本地文件系统路径",function(button,text1){
													if(text1=="")return;
															var requestConfig = {
														url : 'hdfsview.svt?kind=movetolocal',
														params : {
														src:cpath,
														tar:text1
														},
														callback : function(
																option,
																success,
																response) {
															var msg = [
																	success,
																	option.customer,
																	response.responseText];
															if(response.responseText == "1"){
																Ext.MessageBox.alert("Prompt","Success");
															}else{
																Ext.MessageBox.alert("Prompt","Fail");
															}
														}
													}
													Ext.Ajax
															.request(requestConfig);
													})
												}
											}),
											new Ext.Button(
											{
												text : "删除",
												width : 100,
												height : 45,
												handler : function() {
													if (upath == "") {
														Ext.MessageBox
																.alert(
																		"Prompt!",
																		"Please choose a file or directory!");
														return;
													}
													var path;
													Ext.MessageBox
															.confirm(
																	"Prompt",
																	"Please confirm that you wanna delete : \""
																			+ upath
																			+ "\":",
																	function(
																			button) {
																		if (button == "yes") {
																			var requestConfig = {
																				url : 'hdfsview.svt?kind=delete',
																				params : {
																					cpath : cpath
																				},
																				callback : function(
																						option,
																						success,
																						response) {
																					var msg = [
																							success,
																							option.customer,
																							response.responseText];
																					// 																					alert(msg);
																					//grid.getStore().reload();
																					// 																					alert(response.responseText );
																					if (response.responseText == "1") {
																						Ext.MessageBox
																								.alert(
																										"Prompt",
																										"Delete success!");
																						profilePanel
																								.getRootNode()
																								.reload();
																					} else {
																						Ext.MessageBox
																								.alert(
																										"Prompt",
																										"Failed!");
																					}
																				}
																			}
																			Ext.Ajax
																					.request(requestConfig);
																		} else
																		return;
																	});

												}

											})
							/*,
							new Ext.Button(
							{
								text : "Move From Local",
								width : 100,
								height : 45,
								handler : function() {

									var path;
									Ext.MessageBox
											.prompt(
													"Prompt",
													"Please input path:",
													function(
															button,
															text) {
														if (button == "ok") {
															alert("您的新年愿望是:"
																	+ text);
															path = text;

															var requestConfig = {
																url : 'hdfsview.svt?kind=movetolocal',
																params : {
																/////////////////////////
																},
																callback : function(
																		option,
																		success,
																		response) {
																	var msg = [
																			success,
																			option.customer,
																			response.responseText];
																	alert(msg);
																	//grid.getStore().reload();
																}
															}
															Ext.Ajax
																	.request(requestConfig);
														} else
															alert("你放弃了录入！");
														return;
													});

								}
							})
							/*,
							new Ext.Button(
							{
								text : "Copy To Local",
								width : 100,
								height : 25,
								handler : function() {
									var requestConfig = {
										url : 'hdfsview.svt?kind=copytolocal',
										params : {
										/////////////////////
										},
										callback : function(
												option,
												success,
												response) {
											var msg = [
													success,
													option.customer,
													response.responseText];
											alert(msg);
											//grid.getStore().reload();
										}
									}
									Ext.Ajax
											.request(requestConfig);
								}
							}),
							new Ext.Button(
							{
								text : "Copy From Local",
								width : 100,
								height : 25,
								handler : function() {

									var path;
									Ext.MessageBox
											.prompt(
													"Prompt",
													"Please input path:",
													function(
															button,
															text) {
														if (button == "ok") {
															alert("您的新年愿望是:"
																	+ text);
															path = text;
															var requestConfig = {
																url : 'hdfsview.svt?kind=copyfromlocal',
																params : {
																//////////////////////
																},
																callback : function(
																		option,
																		success,
																		response) {
																	var msg = [
																			success,
																			option.customer,
																			response.responseText];
																	alert(msg);
																	//grid.getStore().reload();
																}
															}
															Ext.Ajax
																	.request(requestConfig);
														} else
															alert("你放弃了录入！");
														return;
													});

								}
							})*/]
						});

				var container_2 = new Ext.Panel({
					title : '文件查看',
					width : 650,
					layout : 'border',
					frame : true,
					region : 'east',
					margins : '5 0 5 5',
					items : [profilePanel, buttons]
				});

				var editPanel = new Ext.Panel(
						{
							title : "信息",
							frame : true,
							region : 'center',
							margins : '5 5 5 5',
							//bodyStyle:'background:#f1f1f1',
							items : [{
								html : '用户'+userContext
							}, {
								html : '选择文件系统树，查看文件内容'
							}, {
								html : '选择文件，进行文件操作'
							}
							
							]
						});

				var c_panel = new Ext.Panel({
					frame : true,
					layout : 'border',
					region : 'north',
					height : 250,
					items : [container_2, editPanel]
				});

				var store = new Ext.data.ArrayStore({
					fields : [{
						name : 'company'
					}, {
						name : 'price',
						type : 'float'
					}, {
						name : 'change',
						type : 'float'
					}, {
						name : 'pctChange',
						type : 'float'
					}, {
						name : 'lastChange',
						type : 'date',
						dateFormat : 'n/j h:ia'
					}]
				});

				//////////////////////////////////////////////////////////////////datasource grid

				var proxy = new Ext.data.HttpProxy({
					url : 'hdfsview.svt?kind=filedata&path=null'
				});
				//定义reader
				var reader = new Ext.data.JsonReader({}, [{
					name : '1'
				}, {
					name : '2'
				}, {
					name : '3'
				}, {
					name : '4'
				}, {
					name : '5'
				}, {
					name : '6'
				}, {
					name : '7',
				}, {
					name : '8'
				}, {
					name : '9'
				}], {
					name : '10'
				});
				//构建Store   
				var store = new Ext.data.Store({
					proxy : proxy,
					reader : reader
				});
				//载入
				store.load();

				dataSourcePanel = new Ext.grid.GridPanel({
					id:'dataSourcePanel',
					store : store,
					frame : true,
					region : 'center',
					store : store,
					columns : [{
						id : '1',
						header : 'Column 1',
						width : 160,
						sortable : true,
						dataIndex : '1'
					}, {
						id : '2',
						header : 'Column 2',
						width : 160,
						sortable : true,
						dataIndex : '2'
					}, {
						id : '3',
						header : 'Column 3',
						width : 160,
						sortable : true,
						dataIndex : '3'
					}, {
						id : '4',
						header : 'Column 4',
						width : 160,
						sortable : true,
						dataIndex : '4'
					}, {
						id : '5',
						header : 'Column 5',
						width : 160,
						sortable : true,
						dataIndex : '5'
					}, {
						id : '6',
						header : 'Column 6',
						width : 160,
						sortable : true,
						dataIndex : '6'
					}, {
						id : '7',
						header : 'Column 7',
						width : 160,
						sortable : true,
						dataIndex : '7'
					}, {
						id : '8',
						header : 'Column 8',
						width : 160,
						sortable : true,
						dataIndex : '8'
					}, {
						id : '9',
						header : 'Column 9',
						width : 160,
						sortable : true,
						dataIndex : '9'
					}, {
						id : '10',
						header : 'Column 10',
						width : 160,
						sortable : true,
						dataIndex : '10'
					}],
					stripeRows : true,
					title : '数据源详情',
					// config options for stateful behavior
					stateful : true,
					stateId : 'grid'
				});

				var viewport = new Ext.Viewport({
					layout : 'border',
					items : [c_panel, dataSourcePanel]
				});

				/***********************************************
				              Listeners
				 ***********************************************/
				dataSourcePanel.addListener('cellclick', cellclick);

				function cellclick(grid, rowIndex, columnIndex, e) {
					var record = grid.getStore().getAt(rowIndex); //Get the Record   
					var selectRows = grid.getSelectionModel().getSelections();
					b_username = selectRows[0].get("username");
					b_url = selectRows[0].get("url");
					b_password = selectRows[0].get("password");
					b_driver = selectRows[0].get("driver");
					b_schemaname = selectRows[0].get("schemaname");

					// 			Ext.MessageBox.alert('show', '当前选中的数据是' + username + gender
					// 					+ usergroup + comments);
				}

			});
</script>
</head>

<body>
</body>
</html>
